package com.tjsinfo.tjsoa.mail.bo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;


import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.QbcDao;
import com.tjsinfo.tjsoa.mail.dao.ITjsFoldMailDao;
import com.tjsinfo.tjsoa.mail.dao.TjsmailDao;
import com.tjsinfo.tjsoa.mail.vo.TjsAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmail;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDraft;
import com.tjsinfo.tjsoa.mail.vo.TjsDraftAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldMail;
import com.tjsinfo.tjsoa.mail.vo.TjsemailReceived;
import com.tjsinfo.tjsoa.mail.vo.Tjsmail;

import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

/**
 * 发送邮件Bo
 * @author wangxiaowei
 *
 */
public class TjsMailBo implements ITjsmailBo{
	private TjsmailDao tjsmailDao;
	private IQbcDao qbcDao;
	private TjsDeleteEmailBo tjsDeleteEmailBo;
	private ITjsFoldMailDao tjsFoldMailDao;
	private final boolean STARTTRUE=true;
	private final boolean STARTFALSE=false;
	
	public ITjsFoldMailDao getTjsFoldMailDao() {
		return tjsFoldMailDao;
	}
	public void setTjsFoldMailDao(ITjsFoldMailDao tjsFoldMailDao) {
		this.tjsFoldMailDao = tjsFoldMailDao;
	}
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public TjsmailDao getTjsmailDao() {
		return tjsmailDao;
	}
	public void setTjsmailDao(TjsmailDao tjsmailDao) {
		this.tjsmailDao = tjsmailDao;
	}
	
	public TjsDeleteEmailBo getTjsDeleteEmailBo() {
		return tjsDeleteEmailBo;
	}
	public void setTjsDeleteEmailBo(TjsDeleteEmailBo tjsDeleteEmailBo) {
		this.tjsDeleteEmailBo = tjsDeleteEmailBo;
	}
	/**
	 * 发送邮件
	 * @param 标题
	 * @param 内容
	 * @param 时间
	 * @param 类型
	 * @param 用户ID
	 * @param 状态
	 * @return 
	 */
	public String addSendMail(String title, String value, Date time,
			Integer type, Integer userid, Integer start,List<TjsAnnex>  annexarray,String username,String sendSql) {
		// TODO Auto-generated method stub
		try {
			Tjsmail tjsmail = new Tjsmail();//邮件
			tjsmail.setTjsmailDatatime(time);
			tjsmail.setTjsmailStart(start);
			tjsmail.setTjsmailTitle(title);
			tjsmail.setTjsmailType(type);
			tjsmail.setTjsmailValue(value);
			tjsmail.setTismailread(1);
			tjsmail.setTismailtag(0);
			tjsmail.setTjsmailUsername(username);
			tjsmail.setSendSql(sendSql);
			
			tjsmail.setTjsmailUserid(userid);
			for (Iterator iterator = annexarray.iterator(); iterator.hasNext();) {
				TjsAnnex tjsAnnex = (TjsAnnex) iterator.next();
				if(tjsAnnex.getAnnexName() !=null)
				{
					tjsAnnex.setUserid(userid);
					tjsmail.addAnnexes(tjsAnnex);
				}
			}
			
			//List<Tjsmail> list = tjsmailDao.findAll();
			//if (list.size()>0) {
			//	tjsmail.setSortid(list.iterator().next().getSortid()+1);
			//}else {
			//	tjsmail.setSortid(1);
			//}
			
			return tjsmailDao.addTjsMail(tjsmail);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		
	}
	/**
	 * 查询本人的已发送的邮件
	 * @param 用户ID
	 * @return
	 */
	public List<Tjsmail> selectSendMail(Integer userid) {
		// TODO Auto-generated method stub
		
		DetachedCriteria criteria  = DetachedCriteria.forClass(Tjsmail.class);
		criteria.add(Expression.eq("tjsUser.id", userid));
		return qbcDao.getCriteria(criteria);
	}
	/**
	 * 向特定的用户发送草稿的邮件
	 * @param 接受的用户ID
	 * @param 邮件实体
	 * @return
	 */
	public boolean sendMailDrafList(Integer userid, Tjsmail tjsmail) {
		// TODO Auto-generated method stub
		try {
			tjsmailDao.save(tjsmail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return STARTFALSE;
	}
	/**
	 * 修改邮件状态
	 * @param 邮件ID
	 * @param 状态
	 * @return
	 */

	public boolean updateSendMailStart(String mailid, Integer start) {
		// TODO Auto-generated method stub
		try {
			Tjsmail tjsmail =  selectOneId(mailid);//邮件
			tjsmail.setTjsmailStart(start);
			tjsmailDao.update(tjsmail);
			if(start==2)
			{
				Tjsmail  received = selectOneId(mailid);
				TjsDeleteEmail deleteEmail = new TjsDeleteEmail();
				deleteEmail.setTismailRead(received.getTjsmailStart());
				deleteEmail.setTismailTag(received.getTismailtag());
				deleteEmail.setTjsmailDatatime(new Date());
				deleteEmail.setTjsmailStart(0);
				deleteEmail.setTjsmailTitle(received.getTjsmailTitle());
				deleteEmail.setTjsmailType(received.getTjsmailType());
				deleteEmail.setTjsmailValue(received.getTjsmailValue());
				deleteEmail.setTjsmailUserid(received.getTjsmailUserid());
				deleteEmail.setTjsmailsendren(received.getTjsmailUsername());
				Set set2 = received.getTjsAnnexes();
				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsAnnex annex = (TjsAnnex) iterator.next();
					TjsDeleteEmailAnnex emailAnnex = new TjsDeleteEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setZhenname(annex.getAnnexZhenName());
					deleteEmail.addAnnexes(emailAnnex);
				}
				
				tjsDeleteEmailBo.addTjsDeleteEmail(deleteEmail);
			}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	/**
	 * 修改邮件类型
	 * @param 邮件ID
	 * @param 类型
	 * @return
	 */

	public boolean updateSendMailType(String mailid, Integer type) {
		// TODO Auto-generated method stub
		try {
			Tjsmail tjsmail =  selectOneId(mailid);//邮件
			tjsmail.setTjsmailType(type);
			tjsmailDao.update(tjsmail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}

	/**
	 * 查询一个已发送邮件
	 * @param id
	 * @return
	 */
	public Tjsmail selectOneId(String id)
	{
		return   tjsmailDao.selectByMailId(id).get(0);//邮件
	}
	
	/**
	 * 查询指定用户的已发送的邮件
	 * @param 用户ID
	 * @param 状态ID
	 * @return
	 */
	public List<Tjsmail> selectTjsmail(User user, Integer start) {
		// TODO Auto-generated method stub
		List<Tjsmail> list = tjsmailDao.selectByUserName(user.getUsername(), start);//邮件
		Set<Tjsmail> set = new HashSet<Tjsmail>(list);
		list = new ArrayList<Tjsmail>(set);
		Comparator comp = new Comparator(){
	          public int compare(Object o1,Object o2) {
	        	  Tjsmail p1=(Tjsmail)o1;
	        	  Tjsmail p2=(Tjsmail)o2; 
	            
	        		if (p1.getSortid() < p2.getSortid()) {
	              		return 1;
					} else {
						return 0;
					}
	             }
	        };
	        Collections.sort(list, comp);
		return list;
	}
	/**
	 * 更新已发送邮件的状态
	 * @param 邮件数组
	 * @param 状态
	 * @return
	 */
	public boolean updateTjsmailStart(String[] array,Integer start)
	{
		try {
			for (int i = 0; i < array.length; i++) {
				Tjsmail tjsmail = tjsmailDao.selectByID(array[i]);//邮件
				tjsmail.setTjsmailStart(start);
				tjsmailDao.update(tjsmail);
			}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
  /**
   * 修改邮件的优先级
   * @param array
   * @param type
   * @return
   */
  public boolean updateTjsmailType(String[] array,Integer type)
  {
	  try {
			for (int i = 0; i < array.length; i++) {
				Tjsmail tjsmail = tjsmailDao.selectByID(array[i]);//邮件
				tjsmail.setTjsmailType(type);
				tjsmailDao.update(tjsmail);
			}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
  }
  /**
   * 更新发送邮件的标签
   * @param array
   * @param tag
   * @return
   */
  public boolean updateTjsmailTag(String[] array,Integer tag)
  {
	  try {
			for (int i = 0; i < array.length; i++) {
				Tjsmail tjsmail = tjsmailDao.selectByID(array[i]);//邮件
				tjsmail.setTismailtag(tag);
				tjsmailDao.update(tjsmail);
			}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
  }
  /**
   * 删除发件箱
   * @param array
   * @return
   */
  public boolean deleteTjsmail(String[] array)
  {
	  try {
			for (int i = 0; i < array.length; i++) {
				Tjsmail  received = selectOneId(array[i] );//邮件
				TjsDeleteEmail deleteEmail = new TjsDeleteEmail();//删除邮件
				deleteEmail.setTismailRead(received.getTjsmailStart());
				deleteEmail.setTismailTag(received.getTismailtag());
				deleteEmail.setTjsmailDatatime(new Date());
				deleteEmail.setTjsmailStart(0);
				deleteEmail.setTjsmailTitle(received.getTjsmailTitle());
				deleteEmail.setTjsmailType(received.getTjsmailType());
				deleteEmail.setTjsmailValue(received.getTjsmailValue());
				deleteEmail.setTjsmailUserid(received.getTjsmailUserid());
				Set set2 = received.getTjsAnnexes();//邮件附件
				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsAnnex annex = (TjsAnnex) iterator.next();
					TjsDeleteEmailAnnex emailAnnex = new TjsDeleteEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setZhenname(annex.getAnnexZhenName());
					deleteEmail.addAnnexes(emailAnnex);
				}
				
				tjsDeleteEmailBo.addTjsDeleteEmail(deleteEmail);
				tjsmailDao.delete(received);
			}
				return STARTTRUE;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
  }
  
 /**
  * 移动已发送邮件
  */ 
public boolean moveTjsmail(String[] drafindarray, String foldid) {
	// TODO Auto-generated method stub
	try {
		for (int i = 0; i < drafindarray.length; i++) {
			Tjsmail received = selectOneId(drafindarray[i]);//邮件
			TjsFoldMail tjsFoldMail = new TjsFoldMail();//文件夹邮件
			tjsFoldMail.setTjsmailRead(received.getTismailread());
			tjsFoldMail.setTjsmailTag(received.getTismailtag());
			tjsFoldMail.setTjsmailDatetime(new Date());
			tjsFoldMail.setTjsmailStart(0);
			tjsFoldMail.setTjsmailTitle(received.getTjsmailTitle());
			tjsFoldMail.setTjsmailType(received.getTjsmailType());
			tjsFoldMail.setTjsmailValue(received.getTjsmailValue());
			TjsEmailFolder tjsEmailFolder = new TjsEmailFolder();
			tjsEmailFolder.setFolderId(foldid);
			tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
			//tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
			//tjsFoldMail.setTjsUser(received.getTjsUser());
			tjsFoldMail.setTjsmailUsername(received.getTjsmailUsername());
			Set set2 = received.getTjsAnnexes();//邮件附件
			for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
				TjsAnnex annex = (TjsAnnex) iterator.next();
				TjsFoldEmailAnnex   emailAnnex = new TjsFoldEmailAnnex();
				emailAnnex.setAnnexZhenName(annex.getAnnexZhenName());
				emailAnnex.setAnnexAddress(annex.getAnnexAddress());
				emailAnnex.setAnnexName(annex.getAnnexName());
				emailAnnex.setAnnexSize(annex.getAnnexSize());
				tjsFoldMail.addTjsFoldMailAnnex(emailAnnex);
			}
			tjsFoldMailDao.save(tjsFoldMail);
			tjsmailDao.delete(received);
		}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 查询已发邮件
	 * @param id
	 * @return
	 */
	public List<Tjsmail> selectByID(String id)
	{
		return  tjsmailDao.selectByMailId(id);
	}
	  /**
	 * 查询发送邮件
	 * @param 标题
	 * @param 用户ID
	 * @return
	 */
	public List<Tjsmail> selectByTitle(String title,Integer userid)
	{
		Object[] object = {"%"+title+"%",userid};
		return tjsmailDao.selectByTitle(object);
	}
	
	/**
	 * 修改已发送邮件读写状态
	 * @param 邮件ID
	 * @param 状态
	 * @return
	 */
	public boolean updateSendMailRead(String mailid,Integer read)
	{
		try {
			Tjsmail tjsmail =	selectOneId (mailid);
			tjsmail.setTismailread(read);
			tjsmailDao.update(tjsmail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
}

